package com.zrwl.poscloud.commons.utils;

import org.apache.commons.lang.StringUtils;

/**
 * 组织工具类
 *
 * @className DeptUtil
 */
public class DeptUtil {

    static int COMPANY_LENGTH = 17;
        /* 组织机构分级 */
    /**
     * 0级
     **/
    static final String COMPANY_LVL_HEAD = "0";

    static final int COM_HEAD_START = 0;
    static final int COM_HEAD_LENGTH = 5;

    /**
     * 1级
     **/
    static final String COMPANY_LVL_SECOND = "1";

    static final int COM_SECOND_START = 5;
    static final int COM_SECOND_LENGTH = 3;

    /**
     * 2级
     **/
    static final String COMPANY_LVL_THIRD = "2";

    static final int COM_THIRD_START = 8;
    static final int COM_THIRD_LENGTH = 3;

    /**
     * 3级
     **/
    static final String COMPANY_LVL_FOUTH = "3";

    static final int COM_FOUTH_START = 11;
    static final int COM_FOUTH_LENGTH = 3;

    /**
     * 4级
     **/
    static final String COMPANY_LVL_FIVTH = "4";

    static final int COM_FIVTH_START = 14;
    static final int COM_FIVTH_LENGTH = 3;

    /**
     * 根据给定的编码获取完整编码
     *
     * @param comCode
     * @return
     * @methodName getCompanyFull
     * @author hllian
     * @date May 23, 2014 5:53:18 PM
     */
    public static String getCompanyFull(String comCode) {
        // 补0长度
        int length = COMPANY_LENGTH - comCode.length();
        // 补0字符串
        String tmpStr = StringUtils.leftPad("", length, "0");
        // 合并字符串
        comCode = comCode + tmpStr;
        return comCode;
    }

    /**
     * 截取不带零的最近级别的组织编码
     *
     * @param comCode
     * @return
     * @methodName getLatestCompnay
     * @author hllian
     * @date May 23, 2014 6:02:41 PM
     */
    public static String getLatestCompnay(String comCode) {
        String level = getCompanyLevel(comCode);
        return getCompanyShortCode(comCode, level);
    }

    /**
     * 截取不带零的顶级code
     *
     * @param comCode
     * @return
     * @methodName getHeadCompnay
     * @author hllian
     * @date May 23, 2014 6:02:41 PM
     */
    public static String getHeadCompnay(String comCode) {
        return getCompanyShortCode(comCode, COMPANY_LVL_HEAD);
    }

    /**
     * 获取对应的级别
     *
     * @param comCode
     * @return
     * @methodName getCompanyLevel
     * @author hllian
     * @date May 23, 2014 7:01:37 PM
     */
    public static String getCompanyLevel(String comCode) {
        String level = "";
        if (comCode.length() == 5) return COMPANY_LVL_HEAD;
        String tempCom = comCode.substring(COM_FIVTH_START, COM_FIVTH_START + COM_FIVTH_LENGTH);
        if ("000".equals(tempCom)) {
            tempCom = comCode.substring(COM_FOUTH_START, COM_FOUTH_START + COM_FOUTH_LENGTH);
        } else {
            level = COMPANY_LVL_FIVTH;
            return level;
        }
        if ("000".equals(tempCom)) {
            tempCom = comCode.substring(COM_THIRD_START, COM_THIRD_START + COM_THIRD_LENGTH);
        } else {
            level = COMPANY_LVL_FOUTH;
            return level;
        }
        if ("000".equals(tempCom)) {
            tempCom = comCode.substring(COM_SECOND_START, COM_SECOND_START + COM_SECOND_LENGTH);
        } else {
            level = COMPANY_LVL_THIRD;
            return level;
        }
        if ("000".equals(tempCom)) {
            level = COMPANY_LVL_HEAD;
        } else {
            level = COMPANY_LVL_SECOND;
        }
        return level;
    }

    /**
     * 根据级别截取对应区段的CODE
     *
     * @param comCode
     * @param comLvl
     * @return
     * @methodName getCompanyShortCode
     * @author hllian
     * @date May 23, 2014 6:20:16 PM
     */
    public static String getCompanyShortCode(String comCode, String comLvl) {
        String company = "";
        if (COMPANY_LVL_HEAD.equals(comLvl)) {
            company = comCode.substring(0, COM_HEAD_START + COM_HEAD_LENGTH);
        } else if (COMPANY_LVL_SECOND.equals(comLvl)) {
            company = comCode.substring(0, COM_SECOND_START + COM_SECOND_LENGTH);
        } else if (COMPANY_LVL_THIRD.equals(comLvl)) {
            company = comCode.substring(0, COM_THIRD_START + COM_THIRD_LENGTH);
        } else if (COMPANY_LVL_FOUTH.equals(comLvl)) {
            company = comCode.substring(0, COM_FOUTH_START + COM_FOUTH_LENGTH);
        } else if (COMPANY_LVL_FIVTH.equals(comLvl)) {
            company = comCode.substring(0, COM_FIVTH_START + COM_FIVTH_LENGTH);
        }
        return company;
    }

    /**
     * 根据编码获取第一级主的完整编码
     *
     * @param comCode
     * @return
     * @methodName getHeadCompany
     * @author hllian
     * @date Jun 22, 2014 10:29:14 AM
     */
    static String getHeadCompany(String comCode) {
        return getCompanyFull(getCompanyShortCode(comCode, COMPANY_LVL_HEAD));
    }

    /**
     * 获取下一级级别
     *
     * @param comCode
     * @return 下一级别代码，如果不存在，返回NULL
     * @methodName getChildLevel
     * @author hllian
     * @date Sep 2, 2014 4:02:58 PM
     */
    static String getChildLevel(String comCode) {
        String level = getCompanyLevel(comCode);
        if (level.equals(COMPANY_LVL_FIVTH)) {
            return null;
        } else {
            return Integer.parseInt(level) + 1 + "";
        }
    }

    /**
     * 获取对应级别的对应长度的编码
     *
     * @param comCode
     * @param comLvl
     * @return
     * @methodName getComCodeOfLevel
     * @author hllian
     * @date Sep 2, 2014 4:12:53 PM
     */
    static String getComCodeOfLevel(String comCode, String comLvl) {
        String company = "";
        if (COMPANY_LVL_HEAD.equals(comLvl)) {
            company = comCode.substring(COM_HEAD_START, COM_HEAD_START + COM_HEAD_LENGTH);
        } else if (COMPANY_LVL_SECOND.equals(comLvl)) {
            company = comCode.substring(COM_SECOND_START, COM_SECOND_START + COM_SECOND_LENGTH);
        } else if (COMPANY_LVL_THIRD.equals(comLvl)) {
            company = comCode.substring(COM_THIRD_START, COM_THIRD_START + COM_THIRD_LENGTH);
        } else if (COMPANY_LVL_FOUTH.equals(comLvl)) {
            company = comCode.substring(COM_FOUTH_START, COM_FOUTH_START + COM_FOUTH_LENGTH);
        } else if (COMPANY_LVL_FIVTH.equals(comLvl)) {
            company = comCode.substring(COM_FIVTH_START, COM_FIVTH_START + COM_FIVTH_LENGTH);
        }
        return company;
    }

    /**
     * 检查是否存在对应位置的Code
     *
     * @param comCode 要检查的ComCode
     * @param comLvl  要检查的级别
     * @return boolean 存在返回True，不存在返回False
     * @MethodName existLevel
     * @author hllian
     * @Date Feb 6, 2015 10:17:34 AM
     */
    static boolean existLevel(String comCode, String comLvl) {
        String lvlCode = getComCodeOfLevel(comCode, comLvl);

        String compareCode = "000";
        if (comLvl.equals(COMPANY_LVL_HEAD)) {
            compareCode = "00000";
        }

        if (lvlCode == null || lvlCode.equals("") || lvlCode.equals(compareCode)) {
            return false;
        } else {
            return true;
        }
    }

    public static void main(String[] args) throws Exception {
        String comCode = "10010002003010000";
        System.out.println(existLevel(comCode, COMPANY_LVL_FIVTH));
        System.out.println(getCompanyFull(getCompanyShortCode(comCode, String.valueOf(Integer.parseInt(getCompanyLevel(comCode))-1))));
    }
}
